package com.ex.orage2app;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



import java.util.ArrayList;
import java.util.List;

public class ShoppingCartFragment extends Fragment implements View.OnClickListener {
    private Button walkButton;
    private List<ShoppingCart> cartList = new ArrayList<>();
    private ProductFragment productFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        if (cartList.size() > 0) {
            //有商品
            view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false);
        } else {
            view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false);
        }
        init(view);
        return view;
    }

    /**
     * 组件初始化
     */
    private void init(View view) {
        walkButton = view.findViewById(R.id.random_search);
        walkButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.random_search) {//开启事务，fragment的控制是由事务来实现的
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            if (productFragment == null) {
                productFragment = new ProductFragment();
            }
            transaction.replace(R.id.main_content, productFragment);
            transaction.commit();
        }
    }
}
